package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private RuntimeService runtimeService ;
    @Autowired
    private IBpmnInfoService bpmnInfoService ;



    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        serviceItem.setCreateTime(new Date());
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldObj = this.get(serviceItem.getId());
        if (ServiceItem.SALESTATUS_ON.equals(oldObj.getSaleStatus())) {
            throw new BusinessException("上架服务项目不能修改,请下架后再修改");
        } else if (ServiceItem.AUDITSTATUS_AUDITING.equals(oldObj.getAuditStatus())) {
            throw new BusinessException("项目正在审核过程种,不能被修改");
        }
        if (ServiceItem.AUDITSTATUS_APPROVED.equals(oldObj.getAuditStatus())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_APPROVED);
        }
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOff(Long id) {
        ServiceItem serviceItem = this.get(id);
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) {
            serviceItemMapper.changeAuditStatus(id, ServiceItem.AUDITSTATUS_INIT);
        }

        serviceItemMapper.changeSaleStatus(id, ServiceItem.SALESTATUS_OFF);
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = this.get(id);
        int carPackage = serviceItem.getCarPackage();
        Integer auditStatus = serviceItem.getAuditStatus();
        if (ServiceItem.CARPACKAGE_YES.equals(carPackage) && !ServiceItem.AUDITSTATUS_APPROVED.equals(auditStatus)) {
            throw new BusinessException("套餐需要审核通过后才能上架");
        }
        serviceItemMapper.changeSaleStatus(id, ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void startAudit(Long serviceItemId, Long bpmnInfoId, Long director, Long finance, String info) {
        //传入的id查询服务项,服务项状态处于初始化才可以进行审核
        ServiceItem serviceItem = this.get(serviceItemId);
        if (serviceItem == null && !ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getSaleStatus())) {
            throw new BusinessException("参数异常");
        }

        //修改服务状态为审核中
        serviceItemMapper.changeAuditStatus(serviceItemId, ServiceItem.AUDITSTATUS_AUDITING);

        //  审核信息记录
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setInfo(info);
        carPackageAudit.setServiceItemId(serviceItemId);
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        carPackageAudit.setBpmnInfoId(bpmnInfoId);
        carPackageAudit.setAuditorId(director);
        carPackageAuditService.save(carPackageAudit);

        //  启动流程实例,以金额作为实例的参数
        HashMap<String, Object> map = new HashMap<>();
        if (director != null) {
            map.put("director", director);
        } else if (finance != null) {
            map.put("finance", director);
        }
        map.put("discountPrice", serviceItem.getDiscountPrice());

        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId().toString(), map);


        carPackageAuditService.updateInstanceId(carPackageAudit.getId(),processInstance.getId());



    }

    @Override
    public void changeAuditStatus(Long serviceItemId, Integer auditstatusInit) {
        serviceItemMapper.changeAuditStatus(serviceItemId,auditstatusInit);
    }

}
